﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Configuration;

namespace ZHJPMGR_WebApplication.Handler
{
    /// <summary>
    /// FileUploadHandler 的摘要说明
    /// </summary>
    public class FileUploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile file = context.Request.Files["FileData"];      
            //string timeFormat = System.DateTime.Now.ToLocalTime().Format("yyyyMMddHHmmss");

            string timeFormat = System.DateTime.Now.ToString("yyyyMMddHHmmss");

            //string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\\";
            string uploadpath = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["UploadFileFolder"].ToString()) + "\\";
            string fileName = string.Empty;
            if (file != null)
            {
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }
                int index = file.FileName.LastIndexOf(".");
                fileName = uploadpath + file.FileName.Substring(0, index) + "_" + timeFormat + file.FileName.Substring(index, file.FileName.Length - index - 1);
                file.SaveAs(fileName);
      
                context.Response.Write(fileName);
            }
            else
            {
                context.Response.Write("0");
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}